package cn.wawi.controller;

import java.beans.PropertyEditorSupport;
import java.lang.reflect.ParameterizedType;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wawi.common.ResultData;
import com.wawi.common.StringEscapeEditor;
import com.wawi.common.annotation.MapperName;
import cn.wawi.dao.DaoSupport;
import com.wawi.utils.DateUtil;
import com.wawi.utils.QueryUtil;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
/**
 * <--------------基础控制器-------------->
 * @author 龚亮
 * @date 2015-05-25 14:49:35
 */
@Transactional
public abstract class BaseController<T> {
	
	@Resource
	private DaoSupport dao;
	private String mapperName;
	private boolean isModel=true;
	protected Map<String,Object> paramaterMap=Maps.newHashMap();
	
	@InitBinder
	public void init(WebDataBinder binder) {
		// String类型转换，将所有传递进来的String进行HTML编码，防止XSS攻击、sql注入
		binder.registerCustomEditor(String.class, new StringEscapeEditor(true,false,false));
		
		// Date 类型转换
		binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
			@Override
			public void setAsText(String text) {
				setValue(DateUtil.parseDate(text));
			}
		});
		
		// Timestamp 类型转换
		binder.registerCustomEditor(Timestamp.class, new PropertyEditorSupport() {
			@Override
			public void setAsText(String text) {
				Date date = DateUtil.parseDate(text);
				setValue(date==null?null:new Timestamp(date.getTime()));
			}
		});
	}

	@SuppressWarnings("all")
	public BaseController(){
		try {
			ParameterizedType pt=(ParameterizedType) this.getClass().getGenericSuperclass();
			if(this.getClass().isAnnotationPresent(MapperName.class)){
				MapperName mapper=this.getClass().getAnnotation(MapperName.class);
				mapperName=mapper.value();
				isModel=false;
			}else{
				isModel=true;
				Class<T> clazz=(Class<T>) pt.getActualTypeArguments()[0];
				mapperName=clazz.getName().replaceAll("entity", "dao")+"Dao";
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	/**
	 * 查询所有(不分页)
	 * @return DataGrid
	 */
	@ResponseBody
	@RequestMapping(value="/findAll",method=RequestMethod.GET)
	public ResultData<T> findAll(HttpServletRequest request){
		List<T> list=dao.findAll(mapperName, QueryUtil.setNoPage(request));
		return new ResultData<T>(list,list.size()+0L);
	}

	/**
	 * 分页查询
	 * @return DataGrid
	 */
	@ResponseBody
	@RequestMapping(value="/findAllByPage",method=RequestMethod.GET)
	public ResultData<T> findAllByPage(HttpServletRequest request){
		List<T> list=dao.findAll(mapperName, QueryUtil.setPage(request));
		PageInfo<T> pages = new PageInfo<T>(list);
		return new ResultData<T>(pages.getList(),pages.getTotal());
	}

	/**
	 *根据一组id修改状态
	 */
	@ResponseBody
	@RequestMapping(value="/updateBatch",method=RequestMethod.POST)
	public ResultData<T> updateBatch(String ids){
	    dao.updateBatch(mapperName, ids);
		return new ResultData<T>();
	}
	/**
	 *根据一组id删除
	 */
	@ResponseBody
	@RequestMapping(value="/deleteBatch",method=RequestMethod.POST)
	public ResultData<T> deleteBatch(String ids){
	    dao.deleteBatch(mapperName, ids);
		return new ResultData<T>();
	}
	/**
	 *修改
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value="/updateOne",method=RequestMethod.POST)
	public ResultData<T> updateOne(T t){
		if(isModel){
			dao.updateOne(mapperName, t);
			return new ResultData<T>(t);
		}else{
			dao.updateOne(mapperName, paramaterMap);
			return new ResultData<T>((T)paramaterMap);
		}
	}
	/**
	 *添加
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value="/saveOne",method={RequestMethod.POST,RequestMethod.GET})
	public ResultData<T> saveOne(T t){
		if(isModel){
			dao.saveOne(mapperName, t);
			return new ResultData<T>(t);
		}else{
			dao.saveOne(mapperName, paramaterMap);
			return new ResultData<T>((T)paramaterMap);
		}
	}
	
	@ModelAttribute 
	public void init(HttpServletRequest request){
		paramaterMap.clear();
		paramaterMap=QueryUtil.fileUpload(request);
		System.out.println("param:"+paramaterMap);
	}
}